package com.bestcem.xm.common.core.config.delaymq.job;

import com.bestcem.xm.common.core.config.delaymq.service.DelayMessageService;
import com.bestcem.xm.componet.xxljob.annotation.XxlRegister;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@ConditionalOnProperty(value = "xxl.job.delaymq.enabled", havingValue = "true")
public class DelayMqJobService {

    private static final Logger logger = LoggerFactory.getLogger(DelayMqJobService.class);

    @Resource
    private DelayMessageService delayMessageService;

    @XxlJob(value = "delayMq-pickFromRedis-job")
    //@XxlRegister(cron = "0/1 * * * * ?", triggerStatus = 1, jobDesc = "从redis取到期消息发送到mq")
    @XxlRegister(cron = "${app.common.delayMq.pickFromRedisCron:0/1 * * * * ?}", triggerStatus = 1, jobDesc = "从redis取到期消息发送到mq")
    public void pickFromRedisJob() {
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从redis的zset里得到要到期的mq消息,并将消息投递到mq里 Job开始");
        }*/
        // 调用定时任务
        try {
            delayMessageService.pickMsgFromRedis();
        } catch (Exception e) {
            logger.error("[delayMq] 定时任务pickMsgFromRedis执行异常", e);
        }
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从redis的zset里得到要到期的mq消息,并将消息投递到mq里 Job结束");
        }*/
    }

    @XxlJob(value = "delayMq-pickDb2Redis-job")
    //@XxlRegister(cron = "0 0/1 * * * ?", triggerStatus = 1, jobDesc = "从数据库取7天内的数据发送到redis zset")
    @XxlRegister(cron = "${app.common.delayMq.guardingCron:0 0/1 * * * ?}", triggerStatus = 1, jobDesc = "从数据库取7天内的数据发送到redis zset")
    public void pickDb2RedisJob() {
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库里查到所有待加入到redis的消息,加入redis; Job开始");
        }*/
        // 调用定时任务
        try {
            delayMessageService.pickToToAddRedisMsgFromDataBase();
        } catch (Exception e) {
            logger.error("[delayMq] 定时任务pickToToAddRedisMsgFromDataBase执行异常", e);
        }
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库里查到所有待加入到redis的消息,加入redis; Job结束");
        }

        if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库里查到所有超时但未从redis发送出去的消息 Job开始");
        }*/
        // 调用定时任务
        try {
            delayMessageService.pickToToSendMsgFromDataBase();
        } catch (Exception e) {
            logger.error("[delayMq] 定时任务pickToToSendMsgFromDataBase执行异常", e);
        }
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库里查到所有超时但未从redis发送出去的消息 Job结束");
        }


        if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库查到所有发送失败的消息,进行补发; Job开始");
        }*/
        // 调用定时任务
        try {
            delayMessageService.pickSendFailMsgFromDataBase();
        } catch (Exception e) {
            logger.error("[delayMq] 定时任务pickSendFailMsgFromDataBase执行异常", e);
        }
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 从数据库查到所有发送失败的消息,进行补发;Job结束");
        }


        if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 删除大于1月的消息 Job开始");
        }*/
        // 调用定时任务
        try {
            delayMessageService.deleteUselessMsg();
        } catch (Exception e) {
            logger.error("[delayMq] 定时任务deleteUselessMsg执行异常", e);
        }
        /*if (logger.isInfoEnabled()) {
            logger.info("[delayMq] 删除大于1月的消息 Job结束");
        }*/
    }
}
